<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="skuId" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.skuId`" :rules="formRules.skuId" class="mb-0px!">
            <el-input disabled v-model="row.skuId" placeholder="请输入skuId" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="模式" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.mode`" :rules="formRules.mode" class="mb-0px!">
            <el-input v-model="row.mode" placeholder="请输入模式,如：鑫享家_鑫球盟主" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="业绩" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.profitMode`"
            :rules="formRules.profitMode"
            class="mb-0px!"
          >
            <el-input v-model="row.profitMode" placeholder="请输入业绩与真实业绩,如：10_0" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="规格名" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.skuName`" :rules="formRules.skuName" class="mb-0px!">
            <el-input disabled v-model="row.skuName" placeholder="请输入规格名" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="市场价" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.marketPrice`"
            :rules="formRules.marketPrice"
            class="mb-0px!"
          >
            <el-input disabled v-model="row.marketPrice" placeholder="请输入市场价" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="零售价" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.salePrice`" :rules="formRules.salePrice" class="mb-0px!">
            <el-input disabled v-model="row.salePrice" placeholder="请输入零售价" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="成本价" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.costPrice`" :rules="formRules.costPrice" class="mb-0px!">
            <el-input disabled v-model="row.costPrice" placeholder="请输入成本价" />
          </el-form-item>
        </template>
      </el-table-column>

      <!--      <el-table-column align="center" fixed="right" label="操作" width="60">-->
      <!--        <template #default="{ $index }">-->
      <!--          <el-button @click="handleDelete($index)" link>—</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>
  </el-form>
  <!--  <el-row justify="center" class="mt-3">-->
  <!--    <el-button @click="handleAdd" round>+ 添加产品规格</el-button>-->
  <!--  </el-row>-->
</template>
<script setup lang="ts">
import { GoodsApi } from '@/api/wo/goods'

const props = defineProps<{
  goodsId: undefined // 产品ID（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.goodsId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return
    }
    try {
      formLoading.value = true
      formData.value = await GoodsApi.getGoodsSkuListByGoodsId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    skuId: undefined,
    skuName: undefined,
    marketPrice: undefined,
    salePrice: undefined,
    costPrice: undefined,
    mode: undefined
  }
  row.goodsId = props.goodsId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>
